﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CompleteReader.Settings
{
    class Settings
    {
        private static pdftron.PDF.PDFViewCtrlPagePresentationMode PAGE_PRESENTATION_MODE_DEFAULT = pdftron.PDF.PDFViewCtrlPagePresentationMode.e_single_page;
        private static bool REMEMBER_LAST_PAGE_DEFAULT = true;
        private static bool BUTTONS_STAY_DOWN_DEFAULT = false;
        private static string EMAIL_SIGNATURE_DEFAULT = "Sent from ";
        private static bool NIGHT_MODE_DEFAULT = false;

        private static string PAGE_PRESENTATION_MODE_SETTING_NAME = "CompleteReader_ContinuousMode";
        private static string REMEMBER_LAST_PAGE_SETTING_NAME = "CompleteReader_RememberLastPage";
        private static string BUTTON_STAY_DOWN_SETTING_NAME = "CompleteReader_ButtonsStayDown";
        private static string EMAIL_SIGNATURE_SETTING_NAME = "CompleteReader_EmailSignature";
        private static string NIGHT_MODE_SETTING_NAME = "CompleteReader_NightMode";
        private static string DISPLAY_NAME = Windows.ApplicationModel.Package.Current.DisplayName;

        private static Windows.Storage.ApplicationDataContainer _LocalSettings;
        private static Windows.Storage.ApplicationDataContainer LocalSettings
        {
            get { 
                if (null == _LocalSettings)
                {
                    _LocalSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
                }
                return _LocalSettings;
            }
        }

        private static Windows.Storage.ApplicationDataContainer _RoamingSettings;
        private static Windows.Storage.ApplicationDataContainer RoamingSettings
        {
            get {
                if (_RoamingSettings == null)
                {
                    _RoamingSettings = _RoamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
                }
                return _RoamingSettings;
            }
        }

        public static void DebugReset()
        {
            RoamingSettings.Values[PAGE_PRESENTATION_MODE_SETTING_NAME] = PAGE_PRESENTATION_MODE_DEFAULT == pdftron.PDF.PDFViewCtrlPagePresentationMode.e_single_continuous;
            RoamingSettings.Values[BUTTON_STAY_DOWN_SETTING_NAME] = BUTTONS_STAY_DOWN_DEFAULT;
            RoamingSettings.Values[EMAIL_SIGNATURE_SETTING_NAME] = EMAIL_SIGNATURE_DEFAULT + Settings.DisplayName;

            RoamingSettings.Values[pdftron.PDF.Tools.ToolManager.AnnotationAuthorNameSettingsString] = "";
            RoamingSettings.Values[pdftron.PDF.Tools.ToolManager.DebugAnnotationAuthorHasBeenAskedSettingsString] = false;
        }

        public static string DisplayName
        {
            get { return DISPLAY_NAME; }
            set { DISPLAY_NAME = value; } // in case you don't want to use the display name in the app.
        }

        public static pdftron.PDF.PDFViewCtrlPagePresentationMode PagePresentationMode
        {
            get
            {
                if (RoamingSettings.Values.ContainsKey(PAGE_PRESENTATION_MODE_SETTING_NAME))
                {
                    if (RoamingSettings.Values[PAGE_PRESENTATION_MODE_SETTING_NAME] is bool)
                    {
                        bool isContinuous = (bool)RoamingSettings.Values[PAGE_PRESENTATION_MODE_SETTING_NAME];
                        if (isContinuous)
                        {
                            return pdftron.PDF.PDFViewCtrlPagePresentationMode.e_single_continuous;
                        }
                        else
                        {
                            return pdftron.PDF.PDFViewCtrlPagePresentationMode.e_single_page;
                        }
                    }
                    else // it's an int
                    {
                        int presMode = (int)RoamingSettings.Values[PAGE_PRESENTATION_MODE_SETTING_NAME];
                        return (pdftron.PDF.PDFViewCtrlPagePresentationMode)presMode;
                    }
                }
                return PAGE_PRESENTATION_MODE_DEFAULT;
            }
            set
            {
                RoamingSettings.Values[PAGE_PRESENTATION_MODE_SETTING_NAME] = (int)value;
            }
        }

        public static bool RememberLastPage
        {
            get
            {
                if (RoamingSettings.Values.ContainsKey(REMEMBER_LAST_PAGE_SETTING_NAME))
                {
                    return (bool)RoamingSettings.Values[REMEMBER_LAST_PAGE_SETTING_NAME];
                }
                return REMEMBER_LAST_PAGE_DEFAULT;
            }
            set
            {
                RoamingSettings.Values[REMEMBER_LAST_PAGE_SETTING_NAME] = value;
            }
        }

        public static bool ButtonsStayDown
        {
            get
            {
                if (RoamingSettings.Values.ContainsKey(BUTTON_STAY_DOWN_SETTING_NAME))
                {
                    return (bool)RoamingSettings.Values[BUTTON_STAY_DOWN_SETTING_NAME];
                }
                return BUTTONS_STAY_DOWN_DEFAULT;
            }
            set
            {
                RoamingSettings.Values[BUTTON_STAY_DOWN_SETTING_NAME] = value;
            }
        }

        public static string EmailSignature
        {
            get
            {
                if (RoamingSettings.Values.ContainsKey(EMAIL_SIGNATURE_SETTING_NAME))
                {
                    return RoamingSettings.Values[EMAIL_SIGNATURE_SETTING_NAME] as string;
                }
                Windows.ApplicationModel.Resources.ResourceLoader loader = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView();
                return string.Format(loader.GetString("Settings_Options_EmailSignature_Default"), Settings.DisplayName);
            }
            set
            {
                RoamingSettings.Values[EMAIL_SIGNATURE_SETTING_NAME] = value;
            }
        }

        public static bool NightMode
        {
            get
            {
                if (LocalSettings.Values.ContainsKey(NIGHT_MODE_SETTING_NAME))
                {
                    return (bool)LocalSettings.Values[NIGHT_MODE_SETTING_NAME];
                }
                return NIGHT_MODE_DEFAULT;
            }
            set
            {
                LocalSettings.Values[NIGHT_MODE_SETTING_NAME] = value;
            }
        }
    }
}
